﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using System.IO;

namespace NHibernate.MappingGenerator
{
	public class GeneratorSettings
	{
		private static string _directoryPath;
		private static string _namespace;
		private static bool _generateFileByTable = true;
        private static bool _generateFileClass = true;
		private static string _assemblyName;
		private static string _idGenerator;

		public static void Init()
		{
			string xmlFile = Path.Combine(Environment.CurrentDirectory, "GeneratorSettings.xml");

			XDocument document = XDocument.Load(xmlFile);

			XElement settings = document.Descendants("GeneratorParameters").FirstOrDefault();

			_directoryPath = settings.Attribute("folderPath").Value;
			_namespace = settings.Attribute("namespace").Value;
			_generateFileByTable = bool.Parse(settings.Attribute("generateMultipleFiles").Value);
            _generateFileClass = bool.Parse(settings.Attribute("generateClassFile").Value);
			_assemblyName = settings.Attribute("assemblyName").Value;
			_idGenerator = settings.Attribute("idGenerator").Value;
		}

		public static void Save()
		{
			string xmlFile = Path.Combine(Environment.CurrentDirectory, "GeneratorSettings.xml");
			XDocument document = XDocument.Load(xmlFile);

			XElement settings = document.Descendants("GeneratorParameters").FirstOrDefault();

			settings.Attribute("folderPath").Value = _directoryPath;
			settings.Attribute("namespace").Value = _namespace;
			settings.Attribute("generateMultipleFiles").Value = _generateFileByTable.ToString() ;
			settings.Attribute("assemblyName").Value = _assemblyName;
			settings.Attribute("idGenerator").Value = _idGenerator;
            settings.Attribute("generateClassFile").Value = _generateFileClass.ToString();

			document.Save(xmlFile);
			Init();
		}

        #region Properties
        public static string DirectoryPath
		{
			get
			{
				if (string.IsNullOrEmpty(_directoryPath))
					_directoryPath = Environment.CurrentDirectory;

				return _directoryPath;
			}

			set
			{
				_directoryPath = value;
			}
		}

		public static string Namespace
		{
			get
			{
				if (string.IsNullOrEmpty(_namespace))
					_namespace = "MyApplication.Domain.Entities";

				return _namespace;
			}

			set
			{
				_namespace = value;
			}
		}

		public static bool GenerateFileByTable
		{
			get
			{
				return _generateFileByTable;
			}

			set
			{
				_generateFileByTable = value;
			}
		}

		public static string AssemblyName
		{
			get
			{
				if (string.IsNullOrEmpty(_assemblyName))
					_assemblyName = "MyApplication.Domain.Entities";

				return _assemblyName;
			}

			set
			{
				_assemblyName = value;
			}
		}

		public static string IdGenerator
		{
			get
			{
				if (string.IsNullOrEmpty(_idGenerator))
					_idGenerator = "identity";

				return _idGenerator;
			}

			set
			{
				_idGenerator = value;
			}
		}

        public static bool GenerateFileClass
        {
            get
            {
                return _generateFileClass;
            }

            set
            {
                _generateFileClass = value;
            }

        }
        #endregion
    } 
}

